import __config from "config/env";
import util from "utils/util";

export const request = (url, method, data, showLoading, headers = {}) => {
  let _url = url;
  //#ifndef H5
  // app请求数据地址
  _url = __config.AppPath + url;
  //#endif
  return new Promise((resolve, reject) => {
    if (showLoading) {
      uni.showLoading({
        title: "加载中...",
        mask: true,
      });
    }
    uni.request({
      url: _url,
      method: method,
      data: data,
      withCredentials: true,
      header: {
        //#ifdef H5
        "client-type": util.isWeiXinBrowser() ? "H5-WX" : "H5", //客户端类型普通H5或微信H5
        "tenant-id": getApp().globalData.tenantId,
        "app-id": getApp().globalData.appId ? getApp().globalData.appId : "", //微信h5有appId
        //#endif
        //#ifdef MP-WEIXIN
        "client-type": "MA", //客户端类型小程序
        "app-id": uni.getAccountInfoSync().miniProgram.appId, //小程序appId
        //#endif
        //#ifdef APP-PLUS
        "client-type": "APP", //客户端类型APP
        "tenant-id": 1681960311516827648,
        // "tenant-id": getApp().globalData.tenantId,
        //#endif
        "third-session": uni.getStorageSync("third_session")
          ? uni.getStorageSync("third_session")
          : "",
        ...headers, // 添加自定义请求头
      },
      success(res) {
        if (showLoading) {
          uni.hideLoading();
        }
        if (res.statusCode == 200) {
          if (res.data.code != 200001) {
            if (res.data.code == 60001 || res.data.code == 60002) {
              if (!getApp().globalData.logining) {
                getApp().globalData.logining = true; //防止同时多个接口触发登录
                if (
                  util.isMiniPg() ||
                  (getApp().globalData.appId && util.isWeiXinBrowser())
                ) {
                  //小程序或公众号H5，删除third_session重新登录
                  uni.removeStorageSync("third_session");
                  // #ifdef MP-WEIXIN
                  uni.navigateTo({
                    url: "/pages/login/auth/auth",
                  });
                  // #endif
                  // #ifdef H5
                  if (util.isWeiXinBrowser()) {
                    window.location.href = "/pages/login/auth/auth.html";
                  } else {
                    uni.navigateTo({
                      url: "/pages/login/login/login",
                    });
                  }
                  // #endif
                } else {
                  //App
                  uni.navigateTo({
                    url: "/pages/login/auth/auth",
                  });
                }
              }
            } else {
              uni.showToast({
                title: res.data.msg,
                icon: "none",
              });
            }
            reject(res.data);
          } else {
            resolve(res.data);
          }
        } else {
          uni.showToast({
            title: "服务器正在维护中!",
            icon: "none",
          });
          reject(res);
        }
      },
      fail(err) {
        if (showLoading) {
          uni.hideLoading();
        }
        uni.showToast({
          title: "请求接口失败",
          icon: "none",
        });
        reject(err);
      },
    });
  });
};

module.exports = {
  request,
  // 创建订单
  merchantRecharge: (data) => {
    return request(`/merchant/recharge`, "post", data, true);
  },
  // 支付接口
  wechatPayParams: (data, headers) => {
    return request(`/merchant/recharge/prepay`, "post", data, true, headers);
  },
  // 更新用户详情
  customerInfo: (data, headers) => {
    return request(`/toc/customerInfo/update`, "post", data, true, headers);
  },
};
